package do

import (
	"micro.tag.server/common"
	"micro.tag.server/database"
	"micro.tag.server/models"
)

const (
	RoomDBTable = "vs_r_specialty"
)

func CreateRoomTag(tag *models.VsRSpecialty) error {
	eg := database.GetDB()
	if eg == nil {
		return common.DBNil
	}

	afterID, err := eg.Table(RoomDBTable).Insert(tag)
	if err != nil {
		return err
	}
	if afterID < 1 {
		return common.DBInsertErr
	}

	return nil
}

func DeleteRoomTag(roomID uint64, tagIDs []uint64) error {
	eg := database.GetDB()
	if eg == nil {
		return common.DBNil
	}

	tag := &models.VsRSpecialty{}
	afterID, err := eg.Table(RoomDBTable).Where("room_id = ?", roomID).In("id", tagIDs).Delete(tag)
	if err != nil {
		return err
	}
	if afterID < 1 {
		return common.DBDeleteErr
	}

	return nil
}

func RoomTags(roomID uint64) ([]*models.VsRSpecialty, error) {
	eg := database.GetDB()
	if eg == nil {
		return nil, common.DBNil
	}

	tags := make([]*models.VsRSpecialty, 0)
	err := eg.Table(RoomDBTable).Where("room_id = ?", roomID).Find(tags)
	if err != nil {
		return nil, err
	}

	return tags, nil
}
